<head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
    <title>Vote</title>
</head>

<body>
<?php if (!isset($_POST['submit'])) { exit('permission denied!'); } ?>

<?php
$id = $_POST['id'];
$name = htmlspecialchars($_POST['name']);
?>

<!--
Table user:
    id:
    name:
    website:
    online:
    get_voted: (nr)
    vote_to: (nr)
Table vote:
    i:
    id:
    vote_to: (id)
-->

<?php
include('conn.php');
$user_query = mysql_query( "SELECT * FROM user WHERE `id` = '$id' AND `name` = '$name' LIMIT 1" )
    or die ('Database query failed!');
if (!$result = mysql_fetch_array($user_query)) {
    echo '<p>Login failed! Click <a href="javascript:history.back(-1);">back</a> to retry.</p>';
} else {
    // if succeeded
    $website    = $result[2];
    $online     = $result[3];
    $get_voted  = $result[4];
    $vote_to    = $result[5];
?>

<p>
    欢迎 <?php echo $name ?>!
    <?php if ($vote_to == 0) { ?>
        复选框可以为你觉得喜欢的网站投票～
    <?php } else { ?>
        更改复选框的内容可以更改投票结果～
    <?php } ?>
</p>

<h2>个人信息</h2>

<p>在此录入修改各人网站信息：</p>
<form id="user_form" name="user_form" method="post" action="user_result.php">
<input type="hidden" name="id" value="<?php echo $id; ?>" />
<table width="60%" border="1">
    <tr>
        <th width="30%" bgcolor="#CCCCCC" scope="col">学号</th>
        <td width="30%"><?php echo $id; ?></td>
    </tr>
    <tr>
        <th width="30%" bgcolor="#CCCCCC" scope="col">姓名</th>
        <td width="30%"><?php echo $name; ?></td>
    </tr>
    <tr>
        <th width="30%" bgcolor="#CCCCCC" scope="col">网站</th>
        <td width="30%"><input type="text" name="website" id="website" value="<?php echo $website; ?>" /></td>
    </tr>
    <tr>
        <th width="30%" bgcolor="#CCCCCC" scope="col">开放时间 (e.g. 0:00 - 23:59)</th>
        <td width="30%"><input type="text" name="online" id="online" value="
<?php if ($online == null) { echo '全天'; } else { echo $online; } ?>" />
        </td>
    </tr>
    <tr>
        <th width="30%" bgcolor="#CCCCCC" scope="col">Get Voted #</th>
        <td width="30%"><?php echo $get_voted; ?></td>
    </tr>
    <tr>
        <th width="30%" bgcolor="#CCCCCC" scope="col">已投票数</th>
        <td width="30%"><?php echo $vote_to; ?></td>
    </tr>
</table>
<p align="right">
    <input type="submit" name="submit" value="更新信息" />
    <input type="reset"  name="reset"  value="重置" />
</p>
</form>

<h2>学生网占列表</h2>
<p>在此查看所有同学的网站并可以对喜欢的网站进行投票～</p>

<form id="vote_form" name="vote_form" method="post" action="vote_result.php">
<input type="hidden" name="id" value="<?php echo $id; ?>" />
<table width="100%" border="1">
    <tr>
        <th width="5%" bgcolor="#CCCCCC" scope="col">学号</th>
<!--        <th width="5%" bgcolor="#CCCCCC" scope="col">Name</th>  -->
        <th width="8%" bgcolor="#CCCCCC" scope="col">网站</th>
        <th width="8%" bgcolor="#CCCCCC" scope="col">可访问时间</th>
        <th width="8%" bgcolor="#CCCCCC" scope="col">支持票数</th>
        <th width="8%" bgcolor="#CCCCCC" scope="col">已投票数</th>
    </tr>
    <?php $user_query = mysql_query( "SELECT * FROM user" ) or die ('Database query failed!'); ?>
    <?php while ($row = mysql_fetch_row($user_query)) { ?>
    <tr>
        <td><?php echo $row[0]; ?></td>
<!--        <td><?php echo $row[1]; ?></td> -->
        <td><?php
if ($row[2] != null) {
   echo '<a href="';
   if (strstr($row[2], 'http://') == null) { echo 'http://'; }
       echo $row[2].'" target="_blank">'.$row[2].'</a>'; }?></td>
        <td>
<?php
    if ($row[3] == null) {
        echo '全天';
    } else {
        echo $row[3];
    }
?>
        </td>
        <td><?php echo $row[4]; ?></td>
        <td><input type="checkbox" name="vote_to[]" value="<?php echo $row[0]; ?>"
<?php
    if ($id == $row[0]) {
        echo ' disabled="true"';
    }
    $vote_query = mysql_query( "SELECT * FROM vote WHERE `id` = $id" )
        or die ('Database query failed!');
    while ($v = mysql_fetch_row($vote_query)) {
        if ($v[2] == $row[0]) {
            echo ' checked';
            break;
        }
    }
?>
        /></td>
    </tr>
    <?php } ?>
</table>
<p align="right">
    <input type="submit" name="submit" value="提交" />
    <!--<input type="reset"  name="reset"  value="重置" />-->
</p>
</form>
<?php } ?>
</body>
